package com.zelin.controller;

import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageInterceptor;
import com.zelin.pojo.R;
import com.zelin.pojo.Student;
import com.zelin.pojo.vo.StudentVo;
import com.zelin.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ------------------------------
 * 功能：
 * 作者：WF
 * 微信：hbxfwf13590332912
 * 创建时间：2021/11/16-9:42
 * ------------------------------
 */
@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService studentService;
    //1. 查询所有学生
    @GetMapping("/list")
    public R list(){
        //1.1 查询所有学生列表
        List<Student> students = studentService.findAll();
        //1.2 返回
        return R.ok().data("list",students);
    }

    //2. 条件查询带分页
    @PostMapping("/search/{page}/{pageSize}")
    public R search(@PathVariable int page, @PathVariable int pageSize, @RequestBody(required = false) StudentVo vo){
        //2.1 开始分页查询
        PageInfo<Student> pageInfo = studentService.search(page,pageSize,vo);
        //2.2 返回
        return R.ok().data("total",pageInfo.getTotal()).data("rows",pageInfo.getList());
    }

    //3. 添加学生
    @PostMapping("/add")
    public R add(@RequestBody Student student){
        //3.1 添加学生
        studentService.add(student);
        //3.2 返回
        return R.ok();
    }

    //4. 修改学生
    @PutMapping("/update")
    public R update(@RequestBody Student student){
        //4.1 修改学生
        studentService.update(student);
        //4.2 返回
        return R.ok();
    }

    //5. 删除学生
    @DeleteMapping("/delete/{ids}")
    public R delete(@PathVariable Long[] ids){
        //5.1 删除学生
        studentService.delete(ids);
        //5.2 返回
        return R.ok();
    }
}
